/**
 * 
 */
package example.rss;

import javax.swing.JLabel;

/**
 * 
 * 
 * created on 3:22:38 PM
 * 
 * @author robot
 */
public class LabelCreator {

	private boolean extended;
	private HTMLCreator htmlThingie = new SimpleCreator();

	public JLabel feedTitle(RSSFeed feed) {
		String title = htmlThingie.createTitle(feed, Short.MAX_VALUE);
		return createLabel(title);
	}

	public String createTitleText(RSSFeed feed, int width, int height) {
		int chars = calcChars(width, height, feed.getTitle().length());
		return htmlThingie.createTitle(feed, chars);
	}

	public JLabel entry(RSSEntry entry) {
		String html = htmlThingie.createEntry(entry);
		return createLabel(html);
	}

	private int calcChars(int width, int height, int titleLength) {
		int titleLines = (int) Math.ceil(titleLength / 22.0); // 22 = chars per row
		int titleHeight = titleLines * 16; // 16 = row height
		int numOfLines = (height - titleHeight) / 18; // floor; 18 = height of line
		int charsPerLine = width / 7;
		int chars = numOfLines * charsPerLine; // chars per line

		return chars;
	}

	public JLabel entry(RSSEntry entry, int width, int height) {
		String html = htmlThingie.createEntry(entry, calcChars(width, height, 0));
		return createLabel(html);
	}

	private JLabel createLabel(String text) {
		JLabel label = new JLabel(text);
		return label;
	}

	public void setExtended(boolean extended) {
		this.extended = extended;
		if (extended) {
			htmlThingie = new ExtendedCreator();
		} else {
			htmlThingie = new SimpleCreator();
		}
	}

	public boolean isExtended() {
		return extended;
	}

	interface HTMLCreator {
		String createTitle(RSSFeed feed, int chars);

		String createEntry(RSSEntry entry, int calcChars);

		String createEntry(RSSEntry entry);

	}

	class ExtendedCreator extends SimpleCreator implements HTMLCreator {

		protected String createTitleContent(RSSFeed feed, int chars) {
			String data = feed.getDescription();
			data = trimToSize(data, chars);

			String bla = super.createTitleContent(feed, Short.MAX_VALUE);
			bla += "<br>\n";
			bla += "<font color=black face=arial>" + data + "</font><\n";
			return bla;
		}
	}

	class SimpleCreator implements HTMLCreator {

		protected String trimToSize(String data, int chars) {
			if (chars > 0 && chars < data.length()) {
				data = data.substring(0, chars);
				if (data.lastIndexOf(' ') > 0)
					// cut to the last whole word
					data = data.substring(0, data.lastIndexOf(' ')); 
				data += "...";
			}
			if (chars <= 0)
				data = "...";

			return data;
		}

		@Override
		public String createTitle(RSSFeed feed, int chars) {
			String html = "<html>\n";
			// html +=
			// "<div style=\"white-space:nowrap;overflow: hidden; text-overflow:ellipsis;\">";
			html += "<div style=\"overflow: hidden; text-overflow:ellipsis;\">";
			html += createTitleContent(feed, chars);
			html += "</div></html>";
			return html;
		}

		protected String createTitleContent(RSSFeed feed, int chars) {
			return "<b><font color=blue size=14em face=arial>" + feed.getTitle()
					+ "</font></b>\n";
		}

		public String createEntry(RSSEntry entry, int chars) {
			String content = trimToSize(entry.getContent(), chars);
			// strip images
			content = content.replaceAll("<img[*|\\s|\\w|\\D]*>", " ");
			content = content.replaceAll("<img*>", " ");

			String html = "<html>\n";

			html += "<b><font color=blue size=14em face=arial>" + entry.getTitle()
					+ "</font></b><br>\n";
			html += "<i>&nbsp;Author: " + entry.getAuthor() + "</i><br>\n";
			html += content;
			html += "</div></html>";
			return html;
		}

		@Override
		public String createEntry(RSSEntry entry) {
			return createEntry(entry, entry.getContent().length());
		}
	}
}
